#include "Command.h"
#include "CmdMgr.h"
#include "../../IApplication.h"
#include "../ModuleMgr.h"
#include <QMessageBox>

Command::Command(int id, const QString& name, int baseCmd)
	:m_id(id), m_baseCmd(baseCmd), m_name(name)
{
	m_is_authed = true;
}

bool Command::exec(const QString& paramters)
{
    const IModule* module = ModuleMgr::instance().module(m_module);
	if (module)
	{
		QString param = m_paramers;
		if (!param.isEmpty() && param.endsWith("?%%param"))
			param.replace("%%param", paramters);
		else
			param = paramters;

		QByteArray arr = param.toUtf8();
		QByteArray arr2 = m_name.toUtf8();
        module->execCommand({m_id, m_baseCmd, arr.data(), arr2.data()});
	}
	else
		QMessageBox::information(nullptr, "sumscope", "功能开发中，敬请期待");

	return true;
}

bool Command::exec()
{
	//TODO: process it when cmd is not in qbbridge
	//if (m_baseCmd != 0 && false)
	if(!m_paramers.isEmpty())
	{
        const IModule* module = ModuleMgr::instance().module(m_module);
		if (module)
		{
			QByteArray arr = m_paramers.toUtf8();
			QByteArray arr2 = m_name.toUtf8();
            return module->execCommand({m_id, m_baseCmd, arr.data(), arr2.data()});
		}
		else
		{
			QMessageBox::information(nullptr, "sumscope", "功能开发中，敬请期待");
			return false;
		}
			
		
	}

	return exec(nullptr);
}

bool Command::isAuthed()
{
	return m_is_authed;
}

void Command::setModule(const QString& v)
{
	m_module = v;
	if (!m_module.isEmpty())
	{
		//if the module has not beed loaded
	}
}

void Command::commonPage(int id, int ntype)
{
    //TODO
}
